<template>
  <div class="page-container">
    <el-card class="box-card">
      <!-- 筛选表单 -->
      <el-form :inline="true" :model="filterForm" class="demo-form-inline">
        <el-form-item label="用户名">
          <el-input v-model="filterForm.name" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="filterForm.status" placeholder="请选择">
            <el-option label="全部" value="" />
            <el-option label="启用" value="1" />
            <el-option label="禁用" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
          <el-button @click="resetFilter">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮 -->
      <div class="operation-buttons">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          :disabled="multipleSelection.length === 0"
          >删除</el-button
        >
      </div>

      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="姓名" width="120" />
        <el-table-column prop="gender" label="性别" width="80" />
        <el-table-column prop="age" label="年龄" width="80" />
        <el-table-column prop="phone" label="手机号" width="150" />
        <el-table-column prop="email" label="邮箱" width="180" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { getUserList } from "@/api";
import { ElMessage } from "element-plus";

export default {
  setup() {
    const filterForm = ref({
      name: "",
      status: "",
    });

    const tableData = ref([]);
    const loading = ref(false);
    const multipleSelection = ref([]);

    const pagination = ref({
      currentPage: 1,
      pageSize: 10,
      total: 0,
    });

    // 获取用户列表
    const fetchUserList = () => {
      loading.value = true;
      const params = {
        page: pagination.value.currentPage,
        pageSize: pagination.value.pageSize,
        ...filterForm.value,
      };

      getUserList(params)
        .then((res) => {
          tableData.value = res.data.list;
          pagination.value.total = res.data.total;
        })
        .catch(() => {
          ElMessage.error("获取用户列表失败");
        })
        .finally(() => {
          loading.value = false;
        });
    };

    // 搜索
    const onSearch = () => {
      pagination.value.currentPage = 1;
      fetchUserList();
    };

    // 重置筛选
    const resetFilter = () => {
      filterForm.value = {
        name: "",
        status: "",
      };
      onSearch();
    };

    // 表格选择
    const handleSelectionChange = (val) => {
      multipleSelection.value = val;
    };

    // 分页大小变化
    const handleSizeChange = (size) => {
      pagination.value.pageSize = size;
      fetchUserList();
    };

    // 当前页变化
    const handleCurrentChange = (page) => {
      pagination.value.currentPage = page;
      fetchUserList();
    };

    // 新增用户
    const handleAdd = () => {
      ElMessage.info("新增用户");
    };

    // 编辑用户
    const handleEdit = (row) => {
      ElMessage.info(`编辑用户: ${row.name}`);
    };

    // 删除用户
    const handleDelete = (row) => {
      ElMessage.warning(`删除用户: ${row.name}`);
    };

    onMounted(() => {
      fetchUserList();
    });

    return {
      filterForm,
      tableData,
      loading,
      multipleSelection,
      pagination,
      onSearch,
      resetFilter,
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange,
      handleAdd,
      handleEdit,
      handleDelete,
    };
  },
};
</script>

<style scoped>
.operation-buttons {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
